package exce1.exce05;

import java.util.Random;
import java.util.Scanner;

public class Exer03 {
    public static void main(String[] args) {
        String username="admin";
        String password="admin";
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入账号：");
            String name=sc.nextLine();
            System.out.println("请输入密码：");
            String pass=sc.nextLine();
            String st=code();
            System.out.println("请输入验证码"+st+"：");
            String pass1=sc.nextLine();
            if (name.equals(username)&&pass.equals(password)&&st.equalsIgnoreCase(pass1)){
                System.out.println("登录成功，请开始游戏：");
                game();
                break ;
            }else{
                if (i==2){
                    System.out.println("今日已达上限，请明日再来！");
                }else{
                    System.out.println("输入有误，请重新输入（您还剩"+(2-i)+"次机会：");
                }
            }
        }
    }
    public static String code(){
        Random r=new Random();
        char[] ch=new char[52];
        int in=0;
        for (int i = 65,j=97; i < 65+26; i++,j++) {
            ch[in++]=(char)i;
            ch[in++]=(char)j;
        }
        String code="";
        for (int i = 0; i < 4; i++) {
            int k=r.nextInt(52);
            code+=ch[k];
        }
        return code;
    }
    public static void game(){
        Random r=new Random();
        int num=r.nextInt(100)+1;
        int max=100;
        int min=1;
        Scanner sc=new Scanner(System.in);
        System.out.println("请在1~100之间输入一个数：");
        while(true){
            int n=sc.nextInt();
            if (n==num){
                System.out.println("猜对了，喝酒");
                break;
            }else if (n>num&&n<=100){
                max=n;
                System.out.println(min+"~"+max);
            }else if (n<num&&n>=1){
                min=n;
                System.out.println(min+"~"+max);
            }else{
                System.out.println("输入有误，请重新输入（1~100）：");
            }
        }
    }
}
